// 导入网络请求模块
import { instance as http } from '@/utils/http';
// 导入类型 响应式通用类型 具体业务类型
import type { Data } from '@/types/user';
// 医生列表和文章列表相关类型
import type {
  KnowledgeParams,
  KnowledgePage,
  PageParams,
  DoctorPage,
  FirstDept,
  ConsultOrderPreData,
  Consult,
  ConsultOrderItem,
  ConsultOrderListParams,
  ConsultOrderPage,
} from '@/types/consult';

// 获取知识列表数据
export const getlist = async (params: KnowledgeParams) => {
  // 发起请求
  const res = await http.get<KnowledgePage, Data<KnowledgePage>>('/patient/home/knowledge', {
    params,
  });
  return res.data;
};
// 推荐关注医生的page（列表）
export const getDoctorPage = async (params: PageParams) => {
  // 发起请求
  const res = await http.get<DoctorPage, Data<DoctorPage>>('/home/page/doc', { params });
  return res.data;
};
// 关注/取消医生
export const followDoctor = async (id: string) => {
  // 发起请求
  const res = await http.post('/like', { id, type: 'doc' });
  console.log('followDoctor res', res);
  return res.data;
};
// 关注/取消Knowledge
export const followKnowledge = async (id: string) => {
  // 发起请求
  const res = await http.post('/like', { id, type: 'knowledge' });
  console.log('followKnowledge res', res);
  return res.data;
};

// 获取所有诊室数据
export const getAllDept = async () => {
  // 发起请求
  const res = await http.get<FirstDept[], Data<FirstDept[]>>('/dep/all');
  console.log('getAllDept res', res);
  return res.data;
};
// 封装图片上传的网络其请求
export const uploadImage = async (file: File) => {
  const fd = new FormData();
  fd.append('file', file);
  const res = await http.post<{ id: string; url: string }, Data<{ id: string; url: string }>>(
    '/upload',
    fd
  );
  return res.data;
};
// 获取订单准备信息
export const getConsultOrderPre = async (type: 1 | 2 | 3, illnessType: 0 | 1) => {
  const res = await http.get<ConsultOrderPreData, Data<ConsultOrderPreData>>(
    '/patient/consult/order/pre',
    { params: { type, illnessType } }
  );
  return res.data;
};
// 创建极速问诊订单（支付用）
export const createConsultOrder = async (data: Consult) => {
  // 发起请求
  const res = await http.post<{ id: string }, Data<{ id: string }>>('/patient/consult/order', data);
  return res.data;
};
// 获取支付地址  0 是微信  1 支付宝
export const getConsultOrderPayUrl = async (data: {
  paymentMethod: 0 | 1;
  orderId: string;
  payCallback: string;
}) => {
  // 发起请求
  const res = await http.post<{ payUrl: string }, Data<{ payUrl: string }>>(
    '/patient/consult/pay',
    data
  );
  return res.data;
};
// 获取问诊订单详情
export const getConsultOrderDetail = async (orderId: string) => {
  // 发起请求
  const res = await http.get<ConsultOrderItem, Data<ConsultOrderItem>>(
    '/patient/consult/order/detail',
    {
      params: { orderId: orderId },
    }
  );
  return res.data;
};

// 获取处方预览图片地址
export const getPrescriptionPic = async (id: string) => {
  // 发起请求
  const res = await http.get<{ url: string }, Data<{ url: string }>>(
    `/patient/consult/prescription/${id}`
  );
  return res.data;
};
// 获取问诊记录列表
export const getConsultOrderList = async (params: ConsultOrderListParams) => {
  // 发起请求
  const res = await http.get<ConsultOrderPage, Data<ConsultOrderPage>>(
    '/patient/consult/order/list',
    { params }
  );
  return res.data;
};
// 删除问诊订单
export const deleteOrder = async (id: string) => {
  // 发送请求
  const res = await http.delete(`/patient/order/${id}`);
  return res.data;
};
// 取消问诊订单
export const cancelOrder = async (id: string) => {
  // 发请求
  const res = await http.put(`/patient/order/cancel/${id}`);
  return res.data;
};
